package com.skynet.supervision.controller;

import com.skynet.supervision.aop.uac.LoadUserInfo;
import com.skynet.supervision.aop.uac.UserBriefInfo;
import com.skynet.supervision.aop.uac.beans.UACUserInfo;
import com.skynet.supervision.beans.request.users.UserAddRequest;
import com.skynet.supervision.beans.request.users.UserLoginRequest;
import com.skynet.supervision.beans.response.UserLoginResponse;
import com.skynet.supervision.common.Result;
import com.skynet.supervision.common.ResultGeneric;
import com.skynet.supervision.services.UserInfosService;
import com.skynet.supervision.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户")
@RestController
@RequestMapping(value = "/users")
@Slf4j
@Validated
public class UserController {

    private final UserService userService;

    private final UserInfosService userInfosService;


    public UserController(UserService userService, UserInfosService userInfosService) {
        this.userService = userService;
        this.userInfosService = userInfosService;

    }

    /**
     * 用户登录
     */
    @PostMapping(value = "/login")
    @ApiOperation(value = "用户登录")
    public ResultGeneric<UserLoginResponse> login(@RequestBody @Valid UserLoginRequest request) {
        return ResultGeneric.success(userService.login(request));
    }

    /**
     * 校验token
     */
    @PostMapping(value = "/checkToken")
    @ApiOperation(value = "校验token")
    public ResultGeneric<UACUserInfo> checkToken() {
        return ResultGeneric.success(userService.checkToken());
    }


    @PostMapping(value = "/add")
    @ApiOperation(value = "新增用户")
    public Result addUser(@RequestBody @Valid UserAddRequest request) {
        userService.addUser(request);
        return Result.success();
    }

    // 测试动态关联用户信息
    // 以下几种格式的返回结果均能正确关联加载
//    @PostMapping(value = "/testLoadUserInfo")
//    public ResultGeneric<TestLoadInfoRes> testLoadUserInfo() {
//        TestLoadInfoRes res = new TestLoadInfoRes();
//        res.setLeader("1");
//        res.setParticipants("1,48454124");
//        UserInfoCacheUtils.setUserCache("1", new UserBriefInfo("1", "test", "1", "aaaa"));
//        return ResultGeneric.success(res);
//    }
//
//    @PostMapping(value = "/testLoadUserInfo1")
//    public ResultGeneric<PageResults<TestLoadInfoRes>> testLoadUserInfo1() {
//        TestLoadInfoRes res = new TestLoadInfoRes();
//        res.setLeader("1");
//        res.setParticipants("1,48454124");
//        UserInfoCacheUtils.setUserBriefInfoCache("1", new UserBriefInfo("1", "test", "1", "aaaa"));
//        return ResultGeneric.success(PageResults.of(1, 1, 1, Lists.newArrayList(res)));
//    }

    //
//    @GetMapping(value = "/testLoadUserInfo2")
//    public ResultGeneric<List<TestLoadInfoRes>> testLoadUserInfo2() {
//        val user = UACUserContext.currentUserOrAnonymous();
//        log.info("current user , id = {},name = {}", user.getUserId(), user.getName());
//        TestLoadInfoRes res = new TestLoadInfoRes();
//        res.setLeader("1");
//        res.setParticipants("1,48454124");
//        return ResultGeneric.success(Lists.newArrayList(res));
//    }
//
//    @PostMapping(value = "/testLoadUserInfo3")
//    public Result testLoadUserInfo3() {
//        TestLoadInfoRes res = new TestLoadInfoRes();
//        res.setLeader("1");
//        res.setParticipants("1,48454124");
//        UserInfoCacheUtils.setUserCache("1", new UserBriefInfo("1", "test", "1", "aaaa"));
//        return Result.success((Object) res);
//    }

    @Data
    @LoadUserInfo
    public static class TestLoadInfoRes {
        @ApiModelProperty(value = "负责人员ID")
        private String leader;

        @LoadUserInfo("leader")
        @ApiModelProperty(value = "负责人员")
        private String leaderName;

        @ApiModelProperty(value = "参与调查人员ID")
        private String participants;

        @ApiModelProperty(value = "参与调查人员ID")
        @LoadUserInfo("participants")
        private List<UserBriefInfo> participantInfos;

    }

//    /**
//     * 获取负责人，参与人列表信息（仅id,姓名）
//     * @return
//     */
//    @GetMapping(value = "/getleaderList")
//    public ResultGeneric<List<UserItemVo>> getuserList(){
//        return ResultGeneric.success(userInfosService.getuserList());
//    }

}
